﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace Lyon.Utility
{
    /// <summary>
    /// 计算机信息。
    /// </summary>
    public class ComputerInfo
    {
        static ComputerInfo _Current;

        /// <summary>
        /// 当前计算机信息。
        /// </summary>
        static public ComputerInfo Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new ComputerInfo();
                }
                return _Current;
            }
        }

        /// <summary>
        /// CPU标示
        /// </summary>
        public string ProcessorId
        {
            get
            {
                using (ManagementClass managementClass = new ManagementClass("Win32_Processor"))
                {
                    foreach (ManagementObject item in managementClass.GetInstances())
                    {
                        return item["ProcessorId"].ToString();
                        //return item.Properties["ProcessorId"].Value.ToString();
                    }
                }
                return "unknow";
            }
        }
        /// <summary>
        /// 硬盘标示
        /// </summary>
        public string DiskId
        {
            get
            {
                using (ManagementClass managementClass = new ManagementClass("Win32_DiskDrive"))
                {
                    foreach (ManagementObject item in managementClass.GetInstances())
                    {
                        return item["Model"].ToString();
                        //return item.Properties["Model"].Value.ToString();
                    }
                }
                return "unknow";
            }
        }
        /// <summary>
        /// MAC地址
        /// </summary>
        public string MacAddress
        {
            get
            {
                using (ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    foreach (ManagementObject item in managementClass.GetInstances())
                    {
                        if ((bool)item["IPEnabled"])
                            return item["MacAddress"].ToString();
                    }
                }
                return "unknow";
            }
        }



        //public string CpuID;
        //public string MacAddress;
        //public string DiskID;
        //public string IpAddress;
        //public string LoginUserName;
        //public string ComputerName;
        //public string SystemType;
        //public string TotalPhysicalMemory; //单位：M 
        ///// <summary> 
        ///// 操作系统的登录用户名 
        ///// </summary> 
        ///// <returns></returns> 
        //string GetUserName()
        //{
        //    try
        //    {
        //        string st = "";
        //        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        //        ManagementObjectCollection moc = mc.GetInstances();
        //        foreach (ManagementObject mo in moc)
        //        {

        //            st = mo["UserName"].ToString();

        //        }
        //        moc = null;
        //        mc = null;
        //        return st;
        //    }
        //    catch
        //    {
        //        return "unknow";
        //    }
        //    finally
        //    {
        //    }
        //}
        ///// <summary> 
        ///// PC类型 
        ///// </summary> 
        ///// <returns></returns> 
        //string GetSystemType()
        //{
        //    try
        //    {
        //        string st = "";
        //        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        //        ManagementObjectCollection moc = mc.GetInstances();
        //        foreach (ManagementObject mo in moc)
        //        {

        //            st = mo["SystemType"].ToString();

        //        }
        //        moc = null;
        //        mc = null;
        //        return st;
        //    }
        //    catch
        //    {
        //        return "unknow";
        //    }
        //    finally
        //    {
        //    }

        //}
        ///// <summary> 
        ///// 物理内存 
        ///// </summary> 
        ///// <returns></returns> 
        //string GetTotalPhysicalMemory()
        //{
        //    try
        //    {

        //        string st = "";
        //        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        //        ManagementObjectCollection moc = mc.GetInstances();
        //        foreach (ManagementObject mo in moc)
        //        {

        //            st = mo["TotalPhysicalMemory"].ToString();

        //        }
        //        moc = null;
        //        mc = null;
        //        return st;
        //    }
        //    catch
        //    {
        //        return "unknow";
        //    }
        //    finally
        //    {
        //    }
        //}
        ///// <summary> 
        /////  
        ///// </summary> 
        ///// <returns></returns> 
        //string GetComputerName()
        //{
        //    try
        //    {
        //        return System.Environment.GetEnvironmentVariable("ComputerName");
        //    }
        //    catch
        //    {
        //        return "unknow";
        //    }
        //    finally
        //    {
        //    }
        //}
    }
}
